
RISCV_PREFIX   = riscv32-elf-

RISCV_GCC      = $(RISCV_PREFIX)gcc
RISCV_OBJDUMP  = $(RISCV_PREFIX)objdump
RISCV_OBJCOPY  = $(RISCV_PREFIX)objcopy
RISCV_NM       = $(RISCV_PREFIX)nm
MARCH_DEFINES = -march=rv32i -mabi=ilp32
DEFINES		   = -D CSR_TEST=$(CSR_TEST_NUM)
HEXWIDTH	   = 32
OBJCOPYVL2VL   = perl objcopyvl2vl.pl -width $(HEXWIDTH) -offset 0x00000000 -le
RISCV_READELF  = $(RISCV_PREFIX)readelf
LDSCRIPT        = unit.ld
INCLUDEDIR     = .
SRCDIR		   = .
work_dir	   = .
TESTFILENAME	= %
GCC_ARG = -O3

SRCS			= rlx_riscv_boot.S driver_ss.c  perh_reg_lpddr5_ss.c
OBJS			= driver.o rlx_riscv_boot.o reg_lpddr5.o

TARGETS			= test.elf

TARGETS_HEX		= $(TARGETS_VLOG:.vlog=.hex)
TARGETS_VLOG	= $(TARGETS:.elf=.vlog)
TARGETS_OBJDUMP = $(TARGETS:.elf=.objdump)
TARGETS_BIN		= $(TARGETS:.elf=.bin)

RISCV_GCC_OPTS = -static -O3 -mcmodel=medany -fvisibility=hidden -nostdlib -nostartfiles -lc -lgcc -Wl,-Map=$(TARGETS).map

COMPILE_TARGET=$(RISCV_GCC)
	
		
DUMP_INFO=\
    $(RISCV_OBJDUMP) -D $(work_dir)/$< > $(work_dir)/$<.objdump; \
    $(RISCV_READELF) -a $(work_dir)/$< > $(work_dir)/$<.readelf; \
    $(RISCV_NM) $(work_dir)/$< > $(work_dir)/$<.nm; \
    $(RISCV_OBJCOPY) -O verilog $(work_dir)/$< $(work_dir)/$<.bin;

all: objdump

 .PHONY : objdump

	
reg_lpddr5.o : perh_reg_lpddr5_ss.c
	$(COMPILE_TARGET) -c -I$(INCLUDEDIR) $(DEFINES) $(MARCH_DEFINES) $(GCC_ARG) -o $@ $^

rlx_riscv_boot.o : rlx_riscv_boot.S
	$(COMPILE_TARGET) -c -I$(INCLUDEDIR) $(DEFINES) $(MARCH_DEFINES) $(GCC_ARG) -o $@ $^

driver.o : driver_ss.c
	$(COMPILE_TARGET) -c -I$(INCLUDEDIR) $(DEFINES) $(MARCH_DEFINES) $(GCC_ARG) -o $@ $^
	$(COMPILE_TARGET) -S $^ 

testsuite : $(OBJS)
	$(COMPILE_TARGET) $^  $(RISCV_GCC_OPTS) -T$(LDSCRIPT) $(GCC_ARG) -o test.elf 

depend:
	makedepend $(SRCS)

$(TARGETS_VLOG):%.vlog:%.elf
	$(RISCV_OBJCOPY) $^ -O verilog $@

$(TARGETS_BIN):%.bin:%.elf
	$(RISCV_OBJCOPY) $^ -O binary $@

$(TARGETS_HEX):%.hex:%.vlog
	$(OBJCOPYVL2VL) $^ $@

$(TARGETS_OBJDUMP):%.objdump:%.elf
	 $(RISCV_OBJDUMP) -SI --prefix=./ -l -g $^ > $@



hexgen: testsuite $(TARGETS_HEX)


objdump: clean hexgen $(TARGETS_OBJDUMP) $(TARGETS_BIN)


.PHONY : clean
clean :
	-rm -rf  *.o *.bak *nm* *objdump* *readelf* *.out
	-rm -rf *.exe *.elf *.bin
	-rm -f ../hex_tmp/*


